package com.zzptc.myapp8;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.icu.text.UnicodeSetSpanner;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.zzptc.myapp8.entity.User;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    EditText et_sw_name,et_sw_pwd;
    Switch aSwitch_sw;
    Button btn_sw_login;
    boolean flag;
    List<User> list=new ArrayList<>();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        eventListener();
        User user1=new User("admin","123");
        User user2=new User("aaa","123");
        User user3=new User("bbb","123");
        list.add(user1);
        list.add(user2);
        list.add(user3);
    }

    private void eventListener() {
        //自动登录是否改变
        aSwitch_sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(buttonView.isChecked()){
                    flag=true;
                }else{
                    flag=false;
                }
                Toast.makeText(MainActivity.this, ""+isChecked, Toast.LENGTH_SHORT).show();
            }
        });
        btn_sw_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=et_sw_name.getText().toString().trim();
                String pwd=et_sw_pwd.getText().toString().trim();
                SharedPreferences sh = getSharedPreferences("one",MODE_PRIVATE);
                SharedPreferences.Editor edit=sh.edit();

                if(flag){
                    //获取值
                    // SharedPreferences sh = getSharedPreferences("one",MODE_PRIVATE);
                    //SharedPreferences.Editor edit=sh.edit();

                    //如果为真就记住ta(用户名.密码.是否自动登录）
                    // String name=et_sw_name.getText().toString().trim();
                    //String pwd=et_sw_pwd.getText().toString().trim();

                    //记住
                    edit.putString("uname",name);
                    edit.putString("password",pwd);
                    edit.putBoolean("autologin",flag);
                    edit.commit();

                }else {
                    edit.putString("uname","");
                    edit.putString("password","");
                    edit.putBoolean("autologin",false);
                    edit.commit();
                }

                //登录
                login(name,pwd);
            }
        });
    }
    public void login(String username,String password){
        boolean isLogin=false;
        //循环
        for(User u:list){
            if (u.getUsername().equals(username) &&u.getPassword().equals(password)) {
                //找到该用户了
                //Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                isLogin = true;
                break;
            }else {
                isLogin=false;
            }
        }
        if(isLogin){
            Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
        }

    }

    private void init(){
        et_sw_name=findViewById(R.id.et_sw_name);
        et_sw_pwd=findViewById(R.id.et_sw_pwd);
        aSwitch_sw=findViewById(R.id.aSwitch_aoto_login);
        btn_sw_login=findViewById(R.id.btn_sw_login);

    }
}
